package com.rio.volley;


import com.rio.core.Api;
import com.rio.core.BaseTestCase;
import com.rio.core.L;
import com.rio.helper.json.G;
import com.rio.volley.RequestManager;

public class RequestTestCase extends BaseTestCase {
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		RequestManager.getInstance().init(getContext(), false);	
	}
	
	/**
	 * 开始测试
	 * @param task
	 */
	protected void test(Runnable task){
		new Thread(task).start();
		await();
	}
	
	/**
	 * 打印对象
	 * @param data
	 */
	protected void print(Object data){
		L.i(G.toJson(data));
	}

	/**
	 * 打印错误
	 * @param data
	 */
	protected void error(String data){
		L.e(data);
		finish();
	}	
	
	public void post(Api api){
		
		PostRequestManager.connect(api, new RequestEvent<String>() {

			@Override
			public void onPreExecute() {

			}

			@Override
			public void onFail(Exception exception) {
				error(exception.getMessage());

			}

			@Override
			public String doInBackground(String data) {
				return data;
			}

			@Override
			public void onSuccess(String data) {
				finish();

			}

			@Override
			public void onPostExecute() {
				// TODO Auto-generated method stub
				
			}
		});
		await();		
	}
	
	public void get(Api api){
		
		GetRequestManager.connect(api, new RequestEvent<String>() {

			@Override
			public void onPreExecute() {

			}

			@Override
			public void onFail(Exception exception) {
				error(exception.getMessage());

			}

			@Override
			public String doInBackground(String data) {
				return data;
			}

			@Override
			public void onSuccess(String data) {
				finish();

			}

			@Override
			public void onPostExecute() {
				// TODO Auto-generated method stub
				
			}
		});
		await();		
	}
	
}
